package com.yexiong.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.common.dao.BaseDao;
import com.common.entity.CheckRecord;
import com.common.entity.Event;
import com.yexiong.dao.ICheckRecordDao;

public class CheckRecordDaoImpl extends BaseDao implements ICheckRecordDao{

	@Override
	public Object getEntity(ResultSet rs) {
		CheckRecord checkRecord=new CheckRecord();
		try {
			
				checkRecord.setCheckId(rs.getInt("check_id"));
				checkRecord.setCheckStuId(rs.getInt("stu_id"));
				checkRecord.setCheckDate(rs.getDate("check_date"));
				checkRecord.setCheckStartTime(rs.getTimestamp("check_start"));
				checkRecord.setCheckOverTime(rs.getTimestamp("check_over"));
				checkRecord.setSubject(rs.getString("subject"));
				checkRecord.setDetailSubject(rs.getString("detailSub"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return checkRecord;
	}

	@Override
	public int insert(CheckRecord checkRecord) {
		int result=0;
		String sql="INSERT INTO t_check(stu_id,check_date,check_start,check_over,subject,detailSub) VALUES(?,?,?,?,?,?)";
		Object[] objects=new Object[] {checkRecord.getCheckStuId(),checkRecord.getCheckDate(),
					checkRecord.getCheckStartTime(),checkRecord.getCheckOverTime(),checkRecord.getSubject(),checkRecord.getDetailSubject()};
		result=excuteUpdate(sql, objects);
		return result;
	}

	@Override
	public List<CheckRecord> query(String condition, String startTime, String endTime) {
		List<CheckRecord> list=new ArrayList<>();
		String sql="select * from t_check where stu_id like ? and check_date BETWEEN ? AND ?";
		Object[] objects= {"%"+condition+"%",startTime,endTime+" 23:59:59",};
		list=extuteQuery(sql, objects);
		
		return list;
	}

	@Override
	public int delete(String date,int stuId) {
		int result=0;
		String sql="delete from t_check where check_date=? and stu_id=?";
		Object[] objects= {date,stuId};
		result=excuteUpdate(sql, objects);
		return result;
	}

	@Override
	public int getId(String date, int stuId) {
		List<CheckRecord> list=new ArrayList<>();
		String sql="select * from t_check where check_date=? and stu_id=?";
		Object[] objects= {date,String.valueOf(stuId)};
		list=extuteQuery(sql, objects);
		return list.get(0).getCheckId();
		
	}

	@Override
	public boolean findIsDateIn(int stuId, String date) {
		boolean is=false;
		String sql="select * from t_check where check_date=? and stu_id=?";
		Connection connection=null;
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		try {
			connection=getConnection();
			preparedStatement=connection.prepareStatement(sql);
			preparedStatement.setObject(1, date);
			preparedStatement.setObject(2, stuId);
			resultSet=preparedStatement.executeQuery();
			if (resultSet.next()) {
				is=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return is;
	}

	@Override
	public int updateDate(Date start, Date end,int id) {
		String startTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(start);
		String endTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(end);
		String sql="update t_check set check_start=?,check_over=? where check_id=?";
		Object[] objects= {startTime,endTime,id};
		int result=excuteUpdate(sql, objects);
		return result;
		
	}

}
